package com.project.platform.controller;

import com.alibaba.fastjson2.JSONObject;
import com.project.platform.dto.CurrentUserDTO;
import com.project.platform.dto.LoginDTO;
import com.project.platform.dto.RetrievePasswordDTO;
import com.project.platform.dto.UpdatePasswordDTO;
import com.project.platform.entity.User;
import com.project.platform.exception.CustomException;
import com.project.platform.service.AdminService;
import com.project.platform.service.CommonService;
import com.project.platform.service.ShopService;
import com.project.platform.service.UserService;
import com.project.platform.utils.CurrentUserThreadLocal;
import com.project.platform.utils.JwtUtils;
import com.project.platform.vo.ResponseVO;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

//通用的控制器
@RestController
@RequestMapping("/common")
public class CommonController {

    @Resource
    private AdminService adminService;
    @Resource
    private UserService userService;
    @Autowired
    private ShopService shopService;

    //登录
    @PostMapping("login")
    public ResponseVO<String> login(@RequestBody LoginDTO loginDTO) {//LoginDTO是一个映射的类，用来封装从wed请求得到的登录用户信息
        CommonService commonService = getCommonService(loginDTO.getType());

        //CurrentUserDTO和LoginDTO一样，也是用来封装从实体类里获取的数据，为什么不直接用实体类的原因是，里面多了一个type变量，用来分别当前用户的类别
        //因为三种类型的人员，共用一个loginController
        CurrentUserDTO currentUserDTO = commonService.login(loginDTO.getUsername(), loginDTO.getPassword());
        currentUserDTO.setType(loginDTO.getType());
        String token = JwtUtils.generateToken(currentUserDTO);//登录成功后将返回的用户信息生成token
        return ResponseVO.ok(token);
    }

    //注册
    @PutMapping("register")
    public ResponseVO register(@RequestBody JSONObject data) {
        String type = data.getString("type");
        CommonService commonService = getCommonService(type);
        commonService.register(data);
        return ResponseVO.ok();
    }

    //修改当前用户信息

    @PostMapping("updateCurrentUser")
    public ResponseVO updateCurrentUser(@RequestBody CurrentUserDTO currentUserDTO) {
        CommonService commonService = getCommonService(CurrentUserThreadLocal.getCurrentUser().getType());
        commonService.updateCurrentUserInfo(currentUserDTO);
        return ResponseVO.ok();
    }

    //修改密码

    @PostMapping("updatePassword")
    public ResponseVO updatePassword(@RequestBody UpdatePasswordDTO updatePassword) {
        CommonService commonService = getCommonService(CurrentUserThreadLocal.getCurrentUser().getType());
        commonService.updateCurrentUserPassword(updatePassword);
        return ResponseVO.ok();
    }

    //忘记密码

    @PostMapping("retrievePassword")
    public ResponseVO retrievePassword(@RequestBody RetrievePasswordDTO retrievePasswordDTO) {
        CommonService commonService = getCommonService(retrievePasswordDTO.getType());
        commonService.retrievePassword(retrievePasswordDTO);
        return ResponseVO.ok();
    }

//    重置密码

    @PostMapping("resetPassword")
    public ResponseVO resetPassword(@RequestParam String type, @RequestParam Integer id) {
        CommonService commonService = getCommonService(type);
        commonService.resetPassword(id);
        return ResponseVO.ok();
    }


    //获取当前用户
    @GetMapping("currentUser")
    public ResponseVO<CurrentUserDTO> getCurrentUser() {
//        从CurrentUserThreadLocal中获取当前线程绑定的用户信息
        Integer userId = CurrentUserThreadLocal.getCurrentUser().getId();

        CommonService commonService = getCommonService(CurrentUserThreadLocal.getCurrentUser().getType());
        CurrentUserDTO currentUserDTO = new CurrentUserDTO();
        BeanUtils.copyProperties(commonService.selectById(userId), currentUserDTO);
        currentUserDTO.setType(CurrentUserThreadLocal.getCurrentUser().getType());
        return ResponseVO.ok(currentUserDTO);
    }

//    根据类型对应service

    private CommonService getCommonService(String type) {//
        switch (type) {
            case "ADMIN":
                return adminService;
            case "USER":
                return userService;
            case "SHOP":
                return shopService;
            default:
                throw new CustomException("用户类型错误");
        }
    }
}
